﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-26 10:20:33
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region 二级业务类型 数据访问层
	/// <summary>
	///二级业务类型 数据访问层
	/// </summary>	
	internal partial class BizType:IBizType
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.CGTConnectionString;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(BizTypeID),0)+1
                               FROM [BizType]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private BizTypeInfo GetByReader(IDataReader dr)
         {
            BizTypeInfo info = new BizTypeInfo();
            object obj;
            obj=dr["BizTypeID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.BizTypeID = Convert.ToInt32(obj);
            }
            obj=dr["MasterBizTypeID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MasterBizTypeID = Convert.ToInt32(obj);
            }
            obj=dr["BizTypeName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.BizTypeName = obj.ToString();
            }
            obj=dr["BizTypeSort"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.BizTypeSort = Convert.ToInt32(obj);
            }
            obj=dr["IsShowIndex"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsShowIndex = Convert.ToInt32(obj);
            }
            obj=dr["BizTypeIcon"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.BizTypeIcon = obj.ToString();
            }
            obj=dr["BizTypePinYin"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.BizTypePinYin = obj.ToString();
            }
            obj=dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedDate = Convert.ToDateTime(obj);
            }
            obj=dr["UpdatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UpdatedDate = Convert.ToDateTime(obj);
            }
            obj=dr["CreatedUserID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedUserID = Convert.ToInt32(obj);
            }
            obj=dr["UpdatedUserID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UpdatedUserID = Convert.ToInt32(obj);
            }
            obj=dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsActive = Convert.ToInt32(obj);
            }
            return info;
         }
		public int Insert(BizTypeInfo info)
		{
        
        
            string strSql=@"INSERT INTO BizType			
			(			
			[BizTypeID],
			[MasterBizTypeID],
			[BizTypeName],
			[BizTypeSort],
			[IsShowIndex],
			[BizTypeIcon],
			[BizTypePinYin],
			[CreatedDate],
			[UpdatedDate],
			[CreatedUserID],
			[UpdatedUserID],
			[IsActive]
			)
			 VALUES
			(			
			@BizTypeID,
			@MasterBizTypeID,
			@BizTypeName,
			@BizTypeSort,
			@IsShowIndex,
			@BizTypeIcon,
			@BizTypePinYin,
			@CreatedDate,
			@UpdatedDate,
			@CreatedUserID,
			@UpdatedUserID,
			@IsActive
			)";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@BizTypeID",DbType.Int32, info.BizTypeID);
            db.AddInParameter(dbc, "@MasterBizTypeID",DbType.Int32, info.MasterBizTypeID);
            db.AddInParameter(dbc, "@BizTypeName",DbType.String, info.BizTypeName);
            db.AddInParameter(dbc, "@BizTypeSort",DbType.Int32, info.BizTypeSort);
            db.AddInParameter(dbc, "@IsShowIndex",DbType.Int32, info.IsShowIndex);
            db.AddInParameter(dbc, "@BizTypeIcon",DbType.String, info.BizTypeIcon);
            db.AddInParameter(dbc, "@BizTypePinYin",DbType.String, info.BizTypePinYin);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdatedDate",DbType.DateTime, info.UpdatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@CreatedUserID",DbType.Int32, info.CreatedUserID);
            db.AddInParameter(dbc, "@UpdatedUserID",DbType.Int32, info.UpdatedUserID);
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (int)db.GetParameterValue(dbc,"BizTypeID");
         
		}
		
		
        public void Update(BizTypeInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE BizType SET ");
            sbSql.AppendLine("[MasterBizTypeID] = @MasterBizTypeID,");
                
            sbSql.AppendLine("[BizTypeName] = @BizTypeName,");
                
            sbSql.AppendLine("[BizTypeSort] = @BizTypeSort,");
                
            sbSql.AppendLine("[IsShowIndex] = @IsShowIndex,");
                
            sbSql.AppendLine("[BizTypeIcon] = @BizTypeIcon,");
                
            sbSql.AppendLine("[BizTypePinYin] = @BizTypePinYin,");
                
            sbSql.AppendLine("[CreatedDate] = @CreatedDate,");
                
            sbSql.AppendLine("[UpdatedDate] = @UpdatedDate,");
                
            sbSql.AppendLine("[CreatedUserID] = @CreatedUserID,");
                
            sbSql.AppendLine("[UpdatedUserID] = @UpdatedUserID,");
                
            sbSql.AppendLine("[IsActive] = @IsActive");
                
            sbSql.Append(" where  [BizTypeID] = @BizTypeID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@BizTypeID",DbType.Int32, info.BizTypeID);
            db.AddInParameter(dbc, "@MasterBizTypeID",DbType.Int32, info.MasterBizTypeID);
            db.AddInParameter(dbc, "@BizTypeName",DbType.String, info.BizTypeName);
            db.AddInParameter(dbc, "@BizTypeSort",DbType.Int32, info.BizTypeSort);
            db.AddInParameter(dbc, "@IsShowIndex",DbType.Int32, info.IsShowIndex);
            db.AddInParameter(dbc, "@BizTypeIcon",DbType.String, info.BizTypeIcon);
            db.AddInParameter(dbc, "@BizTypePinYin",DbType.String, info.BizTypePinYin);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdatedDate",DbType.DateTime, info.UpdatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@CreatedUserID",DbType.Int32, info.CreatedUserID);
            db.AddInParameter(dbc, "@UpdatedUserID",DbType.Int32, info.UpdatedUserID);
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(int bizTypeID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [BizType]  ");
            strSql.Append("WHERE [BizTypeID]=@BizTypeID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@BizTypeID",DbType.Int32, bizTypeID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<BizTypeInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<BizTypeInfo> list = new List<BizTypeInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "BizType");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    BizTypeInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public BizTypeInfo GetByID(int bizTypeID)
		{
            Model.BizTypeInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [BizType] ");
            strSql.Append("WHERE [BizTypeID]=@BizTypeID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@BizTypeID",DbType.Int32, bizTypeID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}